package cn.mpq.payment.controller;

import cn.mpq.payment.Utils.ListUtil;
import cn.mpq.payment.common.Result;
import cn.mpq.payment.model.Level;
import cn.mpq.payment.model.Reason;
import cn.mpq.payment.model.Text;
import cn.mpq.payment.model.User;
import cn.mpq.payment.service.LevelService;
import cn.mpq.payment.service.ReasonService;
import cn.mpq.payment.service.TextService;
import cn.mpq.payment.service.UserService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    private ReasonService reasonService;
    @Autowired
    private UserService userService;
    @Autowired
    private TextService textService;
    @Autowired
    private LevelService levelService;
    // 获取原因信息
    @RequestMapping(value = "getReason")
    public Result getReason(String encryptedData, String sessionKey, String iv) {
        List<Reason> reasons = reasonService.selectList(new EntityWrapper<Reason>());
        return new Result(0,"获取成功",reasons);
    }
    // 保存用户信息
    @RequestMapping(value = "saveReason")
    public Result saveReason(User user) {
        userService.update(user,new EntityWrapper<User>().eq("openid",user.getOpenid()));
        return new Result(0,"保存成功",user);
    }
    // 获取文本信息
    @RequestMapping(value = "getTextById")
    public Result getTextById(Integer id) {
        Text text = textService.selectById(id);
        return new Result(0,"保存成功",text);
    }
    // 获取文本信息
    @RequestMapping(value = "getLevel")
    public Result getLevel() {
        List<Level> levels = levelService.selectList(new EntityWrapper<Level>());
        return new Result(0,"保存成功", ListUtil.splistList(levels,9));
    }
}
